package itbour.onetouchshow.adapter;

import android.content.Context;
import android.view.View;

import java.text.SimpleDateFormat;

import itbour.onetouchshow.R;
import itbour.onetouchshow.adapter.base.BaseAdapterHelper;
import itbour.onetouchshow.adapter.base.normal.QuickAdapter;
import itbour.onetouchshow.bean.MusicInfoBean;

/**
 * Created by guoh on 2018/7/6.
 * 功能描述：
 * 需要的参数：
 */
public class MusicListAdapter extends QuickAdapter<MusicInfoBean> {
    private onItemClickListener itemClickListener;

    public MusicListAdapter(Context context) {
        super(context, R.layout.item_music_list);
    }

    @Override
    protected void convert(BaseAdapterHelper helper, MusicInfoBean item, int position) {

        helper.setBackgroundRes(R.id.iv_status, !item.isPlay() ? R.mipmap.icon_start_play_music : R.mipmap.icon_start_pause_music);
        helper.setVisible(R.id.ll_progress, item.isFullShow());

        helper.getView(R.id.sk_progress).setClickable(false);
        helper.getView(R.id.sk_progress).setEnabled(false);
        helper.getView(R.id.sk_progress).setSelected(false);
        helper.getView(R.id.sk_progress).setFocusable(false);

        helper.setProgress(R.id.sk_progress, item.getProgress());
        helper.setText(R.id.tv_bgm_name, item.getName());
        try {
            helper.setText(R.id.tv_time, new SimpleDateFormat("mm:ss").format(item.getDuration() * 1000) + "");
        } catch (Exception e) {
            helper.setText(R.id.tv_time, "0:00");
        }

        helper.setTag(R.id.ll_status, position)
                .setTag(R.id.tv_use_now, position)
                .setOnClickListener(R.id.ll_status, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClickListener != null) {
                            itemClickListener.onMusicPlayClick(Integer.parseInt(v.getTag().toString()));
                        }
                    }
                })
                .setOnClickListener(R.id.tv_use_now, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClickListener != null) {
                            itemClickListener.onUseNowClick(Integer.parseInt(v.getTag().toString()));
                        }
                    }
                });


    }

    public void setItemClickListener(onItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface onItemClickListener {
        void onMusicPlayClick(int position);

        void onUseNowClick(int position);
    }
}
